package com.ossjk.qlh.order.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.RequestBean;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.dishes.entity.Dishes;
import com.ossjk.qlh.dishes.service.IDishesService;
import com.ossjk.qlh.order.entity.Orders;
import com.ossjk.qlh.order.entity.Otrs;
import com.ossjk.qlh.order.service.IOrdersService;
import com.ossjk.qlh.order.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.*;

/**
 * 订单
 *
 * @author liuxi
 * @since 2020-08-14
 */
@Api(tags = "订单")
@RestController
@RequestMapping("/order/orders")
public class OrdersController extends BaseController {

    @Autowired
    private IOrdersService iOrdersService;

    @ApiOperation(value = "列表")
    @RequiresPermissions("00200000")
    @PostMapping("/list")
    public ResponseBean<Page<Orders>> list(@RequestBody RequestBean requestBean) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<Orders>();
        return responseSuccess(iOrdersService.page(requestBean.getPage(), queryWrapper));
    }


    @ApiOperation(value = "去修改")
    @RequiresPermissions("002000000010")
    @PostMapping("/toUpdate")
    public ResponseBean<Orders> toUpdate(@RequestBody RequestBean requestBean) {
        Orders orders = iOrdersService.getById((Serializable) requestBean.getParam().get("id"));
        if (!CommonUtil.isBlank(orders)) {
            return responseSuccess(orders);
        } else {
            return responseFail();
        }
    }

    @ApiOperation(value = "添加")
    @RequiresPermissions("002000000000")
    @ApiOperationSupport(ignoreParameters = {"id", "crer", "mder", "crtm", "mdtm"})
    @PostMapping("/save")
    public ResponseBean save(@RequestBody RequestBean<Orders> requestBean) {
        if (iOrdersService.save(requestBean.getData())) {
            return responseSuccess();
        } else {
            return responseFail();
        }
    }

    @ApiOperation(value = "修改")
    @RequiresPermissions("002000000010")
    @ApiOperationSupport(ignoreParameters = {"crer", "mder", "crtm", "mdtm"})
    @PostMapping("/update")
    public ResponseBean update(@RequestBody RequestBean<Orders> requestBean) {
        if (iOrdersService.updateById(requestBean.getData())) {
            return responseSuccess();
        } else {
            return responseFail();
        }
    }

    @ApiOperation(value = "删除")
    @RequiresPermissions("002000000020")
    @PostMapping("/remove")
    public ResponseBean delete(@RequestBody RequestBean requestBean) {
        List ids = (List) requestBean.getParam().get("ids");
        if (iOrdersService.removeByIds(ids)) {
            return responseSuccess();
        } else {
            return responseFail();
        }
    }

 @ApiOperation(value = "历史订单")
    @PostMapping("/selectHistory")
    public ResponseBean<Page<historyVo>> selectHistory(@RequestBody RequestBean requestBean) {
        QueryWrapper<historyVo> queryWrapper = new QueryWrapper<historyVo>();
        if(!CommonUtil.isBlank(requestBean.getParam().get("type"))) {
            queryWrapper.eq("o.type",requestBean.getParam().get("type"));
        }

        queryWrapper.eq("o.state",3);
        if(!CommonUtil.isBlank(requestBean.getParam().get("jdtm"))) {
            queryWrapper.like("o.jdtm", requestBean.getParam().get("jdtm"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("eid"))) {
            queryWrapper.eq("o.eid", requestBean.getParam().get("eid"));
        }
        return responseSuccess(iOrdersService.selectOrderHistory(requestBean.getPage(),queryWrapper));
    }

    @ApiOperation(value = "统计菜品数")
    @PostMapping("/countTc")
    public ResponseBean<List> tongji(@RequestBody RequestBean requestBean) {

        Map<Object,Integer> map = new HashMap<>();
        List list=new ArrayList();
        List listData=new ArrayList();
        List<countFc> countFc = countFc(requestBean);
        List<countQc> countQc = countQc(requestBean);
        List<countZc> countZ = countZc(requestBean);
        List<countZs> countZs = countZs(requestBean);
        List<countQt> countQt = countQt(requestBean);
        listData.addAll(countFc);
        listData.addAll(countQc);
        listData.addAll(countZ);
        listData.addAll(countZs);
        for (int i=0;i<countQt.size();i++){
            List list1= (List) JSON.parse(countQt.get(i).getOtrs());
            for (int j=0;j<list1.size();j++){
                list.add(JSON.parseObject(JSON.toJSONString(list1.get(j)),Otrs.class).getName());
            }
        }

        for(Object str:list){
            Integer i = 1; //定义一个计数器，用来记录重复数据的个数
            if(map.get(str) != null){
                i=map.get(str)+1;
            }
            map.put(str,i);
        }
        System.out.println(map.toString());
        list.clear();
        Iterator<Object> iterator = map.keySet().iterator();
        while (iterator.hasNext()){
            String s=iterator.next().toString();
            countQtVo countQt1= new countQtVo();
            countQt1.setOtrs(map.get(s));
            countQt1.setTitle(s);
            list.add(countQt1);
        }
        listData.addAll(list);
        return responseSuccess(listData);
    }




    public List<countZc> countZc(@RequestBody RequestBean requestBean) {
//		where o.state = 3
//		AND o.type = 1 and o.eid='dafzxc' AND o.jdtm LIKE '%2020-08-20%'
        QueryWrapper<countZc> queryWrapperZc = new QueryWrapper<countZc>();

        queryWrapperZc.eq("o.type",1);
        queryWrapperZc.eq("o.state",3);
        if(!CommonUtil.isBlank(requestBean.getParam().get("jdtm"))) {
            queryWrapperZc.like("o.jdtm", requestBean.getParam().get("jdtm"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("eid"))) {
            queryWrapperZc.eq("o.eid", requestBean.getParam().get("eid"));
        }
        return iOrdersService.countZC(queryWrapperZc);
    }


    public List<countFc> countFc(@RequestBody RequestBean requestBean) {
        QueryWrapper<countFc> queryWrapper = new QueryWrapper<countFc>();
        queryWrapper.eq("o.type",1);
        queryWrapper.eq("o.state",3);
        if(!CommonUtil.isBlank(requestBean.getParam().get("jdtm"))) {
            queryWrapper.like("o.jdtm", requestBean.getParam().get("jdtm"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("eid"))) {
            queryWrapper.eq("o.eid", requestBean.getParam().get("eid"));
        }
        return iOrdersService.countFC(queryWrapper);
    }


    public List<countQc> countQc(@RequestBody RequestBean requestBean) {
        QueryWrapper<countQc> queryWrapper = new QueryWrapper<countQc>();
        queryWrapper.eq("o.type",1);
        queryWrapper.eq("o.state",3);
        if(!CommonUtil.isBlank(requestBean.getParam().get("jdtm"))) {
            queryWrapper.like("o.jdtm", requestBean.getParam().get("jdtm"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("eid"))) {
            queryWrapper.eq("o.eid", requestBean.getParam().get("eid"));
        }
        return iOrdersService.countQC(queryWrapper);
    }


    public List<countZs> countZs(@RequestBody RequestBean requestBean) {
        QueryWrapper<countZs> queryWrapper = new QueryWrapper<countZs>();
        queryWrapper.eq("o.type",1);
        queryWrapper.eq("o.state",3);
        if(!CommonUtil.isBlank(requestBean.getParam().get("jdtm"))) {
            queryWrapper.like("o.jdtm", requestBean.getParam().get("jdtm"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("eid"))) {
            queryWrapper.eq("o.eid", requestBean.getParam().get("eid"));
        }
        return iOrdersService.countZs(queryWrapper);
    }

    public List<countQt> countQt(@RequestBody RequestBean requestBean) {
        QueryWrapper<countQt> queryWrapper = new QueryWrapper<countQt>();
        queryWrapper.eq("o.type",1);
        queryWrapper.eq("o.state",3);
        if(!CommonUtil.isBlank(requestBean.getParam().get("jdtm"))) {
            queryWrapper.like("o.jdtm", requestBean.getParam().get("jdtm"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("eid"))) {
            queryWrapper.eq("o.eid", requestBean.getParam().get("eid"));
        }
        return iOrdersService.countQt(queryWrapper);
    }


//    @ApiOperation(value = "列表")
//    @RequiresPermissions("00200000")
//    @PostMapping("/list")
//    public ResponseBean<Page<Orders>> list(@RequestBody RequestBean requestBean) {
//        QueryWrapper<Orders> queryWrapper = new QueryWrapper<Orders>();
//        return responseSuccess(iOrdersService.page(requestBean.getPage(), queryWrapper));
//    }
    @ApiOperation(value = "历史订单明细")
    @PostMapping("/historyJdPrint")
    public ResponseBean<Page<JdOrderVo>> historyJdPrint(@RequestBody RequestBean requestBean) {
        QueryWrapper<JdOrderVo> queryWrapper = new QueryWrapper<JdOrderVo>();

        if (!CommonUtil.isBlank(requestBean.getParam().get("ename"))) {
            queryWrapper.eq("en.ename", requestBean.getParam().get("ename"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("jdtm"))) {
            queryWrapper.like("o.jdtm", requestBean.getParam().get("jdtm"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("type"))) {
            queryWrapper.eq("o.type", requestBean.getParam().get("type"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("state"))) {
            queryWrapper.eq("o.state", requestBean.getParam().get("state"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("code"))) {
            queryWrapper.eq("o.`code`", requestBean.getParam().get("code"));
        }
        if (!CommonUtil.isBlank(requestBean.getParam().get("phone"))) {
            queryWrapper.eq("o.phone", requestBean.getParam().get("phone"));
        }
        return responseSuccess(iOrdersService.historyJdPrint(requestBean.getPage(),queryWrapper));
    }
}
